package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Member;
import com.atguigu.crowd.entity.Project;
import com.atguigu.crowd.entity.ProjectManger;
import com.atguigu.crowd.service.api.AuthProjectService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class AuthProjectHandler {

    @Autowired
    private AuthProjectService authProjectService;

    @RequestMapping("/auth/project/page.html")
    public String getAuthMemberInfo(
            //keyword有时有 有时无 设置默认为空字符串
            @RequestParam(value = "keyword",defaultValue = "") String keyword,
            //pageNum 默认值设为1
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            //pageSize 默认值设为5
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            HttpSession session,
            ModelMap modelMap
    ){
        PageInfo<Project> ProjectPageInfo = authProjectService.getAuthProjectPageInfo(keyword, pageNum, pageSize);


        List<Project> list = ProjectPageInfo.getList();

        session.setAttribute("auth_project",ProjectPageInfo);
        modelMap.addAttribute("auth_project",ProjectPageInfo);

        return  "auth-project";

    }


    //审核通过/不通过删除
    @RequestMapping("/auth/project/change.html")
    public String changeMember(
            //keyword有时有 有时无 设置默认为空字符串
            @RequestParam(value = "keyword",defaultValue = "") String keyword,
            //pageNum 默认值设为1
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            //pageSize 默认值设为5
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            ModelMap modelMap,
            @RequestParam(value = "change")Integer change,
            @RequestParam(value = "projectId")Integer projectId,
            @RequestParam(value = "projectDay",required = false)Integer projectDay,
            HttpSession session
    ){

//        Object auth_project = session.getAttribute("auth_project");

//        System.out.println("change: "+change);
        if(change==1){
            //审核通过
            //修改pass status字段
            authProjectService.updateProjectPass(projectId);


            //随机数 在范围内产生一个随机数
            Random rand = new Random();
            //设置项目经理
            //从项目经理表 获取项目经理总数
            int counts = authProjectService.getCountProjectMangeer();
            System.out.println("获取项目经理总数: "+counts);
            int cycleCounts=0;
            System.out.println("审核 循环开始");
            while (true){
                counts++;
                int random = rand.nextInt(counts)+1;
                System.out.println("生成的随机数： "+random);

                //获取此随机数对应的项目经理
                ProjectManger manger = authProjectService.getMangerCount(random);
                System.out.println("manger:  "+manger);
                int countProject = manger.getCount();
                System.out.println("审核 循环中");
                if(countProject<5){
                    //若项目数<5

                    //设置到项目表中
                    authProjectService.updateProjectManger(projectId,manger.getMangerName());
                    //项目经理count++

                    countProject++;
                    authProjectService.updateMangerCount(random,countProject);
                    break;
                }

                //所有项目经理都满了 直接按上面的 超出也可以 提示超出
                if(cycleCounts >counts){
                    //设置到项目表中
                    authProjectService.updateProjectManger(projectId,manger.getMangerName());
                    //项目经理count++

                    authProjectService.updateMangerCount(random,countProject++);

                    session.setAttribute("message","项目经理已满");
                    break;
                }
            }

            Date now = new Date();
            String deploydate = new SimpleDateFormat("yyyy-MM-dd").format(now);

            //设置众筹开始时间 和 修改development_start_date字段 = 审核成功日期+众筹天数
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.add(Calendar.DATE,+projectDay);
            Date time = cal.getTime();
            String developmentStartDate = new SimpleDateFormat("yyyy-MM-dd").format(time);
            authProjectService.updateProjectDeploydate(projectId,deploydate,developmentStartDate);
        }else if (change==2){
            //审核不通过
            authProjectService.updateProjectNotPass(projectId);
        }

        return "redirect:/auth/project/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }




    //项目详情查看
    @RequestMapping("/auth/more/project/page.html")
    public String showMoreProject(
            @RequestParam(value = "projectId") Integer projectId,
            HttpSession session
    ){
        PageInfo<Project> auth_project = (PageInfo<Project>)session.getAttribute("auth_project");

        //可能还需要回报信息 详情图片等 可以在这里进行数据库操作
        List<Project> list = auth_project.getList();
        for (Project p: list) {
            if(p.getId() == projectId){
                session.setAttribute("auth_project_single",p);
            }
        }
        return "auth-project-more";
    }


}
